package com.drx.controller;

import com.drx.service.TabUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserDetail {
    @Autowired
    TabUsersService tabUsersService;

    @RequestMapping(value = "/beginAddUsers",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,String> beginAddUsers(String[] userNames){
        Map<String,String> result=new HashMap<String, String>();
        Integer addNum= tabUsersService.addUsers(userNames);
        if (addNum!=0){
            result.put("addResult","true");
            result.put("addNum",addNum+"");
        }else{
            result.put("addResult","false");
        }
        return result;
    }

    @RequestMapping(value = "/getAllUserNames",produces = "application/json;charset=utf-8")
    @ResponseBody
    public List<Integer> getAllUserNames(){
        List<Integer> userNames = tabUsersService.selectAllUserNames();
        return userNames;
    }

    @RequestMapping(value = "/beginDeleteUsers",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,String> beginDeleteUsers(String[] deleteUserNames){
        Map<String,String> result=new HashMap<String, String>();
        Integer deleteNum= tabUsersService.deleteUsers(deleteUserNames);
        if (deleteNum!=0){
            result.put("addResult","true");
            result.put("addNum",deleteNum+"");
        }else{
            result.put("addResult","false");
        }
        return result;
    }

    @RequestMapping(value = "/changePassword",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,String> changePassword(Integer studentNum,String newPassword){
        Map<String,String> result=new HashMap<String, String>();
        //调用service层的方法修改密码
        Integer changeNum= tabUsersService.changePassword(studentNum, newPassword);
        if (changeNum!=0){
            result.put("changeResult","true");
        }else{
            result.put("changeResult","false");
            result.put("errorReason","无此用户");
        }
        return result;
    }



}
